package com.local.es.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ配置类
 */
@Configuration
public class RabbitMQConfig {

    @Value("${rabbitmqs.es-goods-exchange}")
    private String esGoodsExchange;

    @Value("${rabbitmqs.es-goods-update-queue}")
    private String esGoodsUpdateQueue;

    @Value("${rabbitmqs.es-goods-update-routing-key}")
    private String esGoodsUpdateRoutingKey;

    @Value("${rabbitmqs.es-goods-delete-queue}")
    private String esGoodsDeleteQueue;

    @Value("${rabbitmqs.es-goods-delete-routing-key}")
    private String esGoodsDeleteRoutingKey;


    @Bean
    public TopicExchange esGoodsExchange() {
        return new TopicExchange(this.esGoodsExchange);
    }

    @Bean
    public Queue esGoodsUpdateQueue() {
        return new Queue(this.esGoodsUpdateQueue);
    }

    @Bean
    public Binding bindEsGoodsUpdateQueueExchange(Queue esGoodsUpdateQueue, TopicExchange esGoodsExchange) {
        return BindingBuilder.bind(esGoodsUpdateQueue).to(esGoodsExchange).with(this.esGoodsUpdateRoutingKey);
    }

    @Bean
    public Queue esGoodsDeleteQueue() {
        return new Queue(this.esGoodsDeleteQueue);
    }

    @Bean
    public Binding bindEsGoodsDeleteQueueExchange(Queue esGoodsDeleteQueue, TopicExchange esGoodsExchange) {
        return BindingBuilder.bind(esGoodsDeleteQueue).to(esGoodsExchange).with(this.esGoodsDeleteRoutingKey);
    }

    /**
     * 对rabbitmq的Object类型的消息进行json处理（Object的bean要实现Serializable接口）
     * @return
     */
    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}
